package easyway.core.utils;

import java.io.Serializable;
import java.util.List;

/**
 * 分页类

* @author zhangqihang
* @date Nor 23, 2009
*/
public class PaginationSupport implements Serializable {
	
	private static final long serialVersionUID = 30161819074846596L;

	/**
	 * 默认每页显示数
	 */
	public final static int PAGESIZE = 10;

	/**
	 * 每页显示数
	 */
	private int pageSize = PAGESIZE;

	/**
	 * 总记录数
	 */
	private int totalResult;

	/**
	 * 开始索引数
	 */
	private int startResult;

	/**
	 * 数组长度为总页数，数组每一维的数值表示每页开始索引数
	 */
	private int indexes;

	/**
	 * 显示项集合
	 */
	@SuppressWarnings("unchecked")
	private List items;
	
	/**
	 * 总页数
	 */
	@SuppressWarnings("unused")
	private int totalCount;

	/**
	 * 
	 * @param items
	 *            显示项集合
	 * @param totalResult
	 *            总记录数
	 */
	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalResult) {
		setPageSize(PAGESIZE);
		setTotalResult(totalResult);
		setStartResult(0);
		setItems(items);
	}

	/**
	 * 
	 * @param items
	 *            显示项集合
	 * @param totalResult
	 *            总记录数
	 * @param startResult
	 *            开始索引数
	 */
	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalResult, int startResult) {
		setPageSize(PAGESIZE);
		setTotalResult(totalResult);
		setStartResult(startResult);
		setItems(items);
	}

	/**
	 * 
	 * @param items
	 *            显示项集合
	 * @param totalResult
	 *            总记录数
	 * @param startResult
	 *            开始索引数
	 * @param pageSize
	 *            每页显示数
	 */
	@SuppressWarnings("unchecked")
	public PaginationSupport(List items, int totalResult, int startResult,
			int pageSize) {
		setPageSize(pageSize);
		setTotalResult(totalResult);
		setStartResult(startResult);
		setItems(items);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalResult() {
		return totalResult;
	}

	/**
	 * 设置总页数indexes.length，总记录数totalResult，每页开始索引数(保存在数组每一维中)
	 * 
	 * @param totalResult 总记录数
	 */
	public void setTotalResult(int totalResult) {
		if (totalResult > 0) {
			this.totalResult = totalResult;
			int count = totalResult / pageSize;
			if (totalResult % pageSize > 0)
				count++;
			indexes = count;
		} else {
			this.totalResult = 0;
		}
	}

	public int getStartResult() {
		return startResult;
	}

	/**
	 * 设置显示页相应的开始索引数
	 * 
	 * @param startResult 开始索引数
	 */
	public void setStartResult(int startResult) {
		if (totalResult <= 0)
			this.startResult = 0;
		else if (startResult >= totalResult)
			this.startResult = indexes - 1;
		else if (startResult < 0)
			this.startResult = 0;
		else
			this.startResult = startResult / pageSize;
	}

	/**
	 * 获得下一页索引
	 * 
	 * @return 下一页索引
	 */
	public int getNextIndex() {
		int nextIndex = getStartResult() + pageSize;
		if (nextIndex >= totalResult)
			return getStartResult();
		else
			return nextIndex;
	}

	/**
	 * 获得上一页索引
	 * 
	 * @return 上一页索引
	 */
	public int getPreviousIndex() {
		int previousIndex = getStartResult() - pageSize;
		if (previousIndex < 0)
			return 0;
		else
			return previousIndex;
	}

	public int getIndexes() {
		return indexes;
	}

	public void setIndexes(int indexes) {
		this.indexes = indexes;
	}

	/**
	 * 获得分页数据
	 * 
	 * @return 分页集合
	 */
	@SuppressWarnings("unchecked")
	public List getItems() {
		return items;
	}

	@SuppressWarnings("unchecked")
	public void setItems(List items) {
		this.items = items;
	}

	public int getTotalCount() {
		return getIndexes();
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	
	/*public static void main(String[] args) {
		PaginationSupport ps = new PaginationSupport(null, 90, 1, 10);
		System.out.println("总记录数：" + ps.getTotalResult());
		System.out.println("总共页数：" + ps.getIndexes().length);
		System.out.println("开始索引：" + ps.getStartResult());
		System.out.println("下一页索引：" + ps.getNextIndex());
		System.out.println("上一页索引：" + ps.getPreviousIndex());
		System.out.println("每页显示数：" + ps.getPageSize());
		for (int i = 0; i < ps.getIndexes().length; i++) {
			System.out.println("第" + (i + 1) + "页从" + ps.getIndexes()[i]
					+ "记录开始显示");
		}
	}*/
	
}
